{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T16:05:53.272639Z",
     "start_time": "2025-01-07T16:05:53.269900Z"
    }
   },
   "source": "import numpy as np",
   "outputs": [],
   "execution_count": 24
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "nan：表示“非数字”（Not a Number），通常用于表示未定义或不可表示的值。\n",
    "\n",
    "nan 不等于任何值，包括它自己。使用 == 比较 nan 会返回 False，必须用 np.isnan() 检测。\n",
    "\n",
    "inf：表示“无穷大”（Infinity），分为正无穷（inf）和负无穷（-inf）。\n",
    "\n",
    "inf 可以参与数学运算，例如 inf + 1 仍然是 inf。\n",
    "\n",
    "inf 与 inf 比较会返回 True\n",
    "\n",
    "在数学运算中，nan 和 inf 会传播。例如，nan + 1 仍然是 nan，inf * 0 是 nan"
   ],
   "id": "dd2bd38d9a8c9b4c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:05:53.291253Z",
     "start_time": "2025-01-07T16:05:53.287604Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建 nan 和 inf\n",
    "a = np.array([1, 2, np.nan, 4, np.inf, -np.inf])\n",
    "print(a)"
   ],
   "id": "905cdd9e01cae825",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1.   2.  nan   4.  inf -inf]\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:05:53.318923Z",
     "start_time": "2025-01-07T16:05:53.315074Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 检测 nan\n",
    "print(np.isnan(a))"
   ],
   "id": "7ba637c7e18d6d11",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False False  True False False False]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:05:53.323519Z",
     "start_time": "2025-01-07T16:05:53.319923Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 检测 inf\n",
    "print(np.isinf(a))"
   ],
   "id": "b40938b1aa22c97c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False False False False  True  True]\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:05:53.338186Z",
     "start_time": "2025-01-07T16:05:53.335490Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 检测有限值\n",
    "print(np.isfinite(a))"
   ],
   "id": "a231e3cce472b57e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True  True False  True False False]\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:05:53.343832Z",
     "start_time": "2025-01-07T16:05:53.339186Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 过滤 nan\n",
    "filtered_nan = a[~np.isnan(a)]\n",
    "print(filtered_nan)"
   ],
   "id": "a83b2072f6665d75",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1.   2.   4.  inf -inf]\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:05:53.348280Z",
     "start_time": "2025-01-07T16:05:53.344832Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 过滤 inf\n",
    "filtered_inf = a[~np.isinf(a)]\n",
    "print(filtered_inf)"
   ],
   "id": "c68a6f960c413bcd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.  2. nan  4.]\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:05:53.352623Z",
     "start_time": "2025-01-07T16:05:53.349280Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 过滤 nan 和 inf\n",
    "filtered_finite = a[np.isfinite(a)]\n",
    "print(filtered_finite)"
   ],
   "id": "2010f9d1286f34ad",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 4.]\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:05:53.385091Z",
     "start_time": "2025-01-07T16:05:53.381549Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 替换 nan 为 0\n",
    "a[np.isnan(a)] = 0\n",
    "print(a)"
   ],
   "id": "1af4ff3bea26ae08",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1.   2.   0.   4.  inf -inf]\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:05:53.407227Z",
     "start_time": "2025-01-07T16:05:53.403048Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 替换 inf 为最大值\n",
    "a[np.isinf(a)] = np.nan  # 先将 inf 替换为 nan\n",
    "a[np.isnan(a)] = 999     # 再将 nan 替换为 999\n",
    "print(a)"
   ],
   "id": "378ebea0750d83a8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1.   2.   0.   4. 999. 999.]\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:05:53.436927Z",
     "start_time": "2025-01-07T16:05:53.433160Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([1, 2, np.nan, 4, 5])\n",
    "# 忽略 nan 计算均值\n",
    "mean = np.nanmean(a)\n",
    "print(mean)  "
   ],
   "id": "2e943804b3f05fe0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:05:55.749657Z",
     "start_time": "2025-01-07T16:05:55.745384Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 忽略 nan 计算总和\n",
    "total = np.nansum(a)\n",
    "print(total)"
   ],
   "id": "b1188b444fd8a698",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12.0\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "将数组中nan值去除，替换为对应列的平均值",
   "id": "3586ca31745281af"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:22:09.922348Z",
     "start_time": "2025-01-07T16:22:09.918238Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建含有 nan 的二维数组\n",
    "data = np.array([[1, 2, np.nan],\n",
    "                 [4, np.nan, 6],\n",
    "                 [7, 8, 9]])\n",
    "print(\"原始数组:\\n\", data)"
   ],
   "id": "5691cd697deddf9f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组:\n",
      " [[ 1.  2. nan]\n",
      " [ 4. nan  6.]\n",
      " [ 7.  8.  9.]]\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:22:11.936274Z",
     "start_time": "2025-01-07T16:22:11.931821Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 替换为列平均值，利用np.nanmean()、np.isnan()、np.where()、np.take()\n",
    "col_mean = np.nanmean(data, axis=0)# 通过 np.nanmean() 可以计算忽略 nan 的平均值\n",
    "nan_indices = np.isnan(data)# 使用 np.isnan() 和 np.where() 可以定位 nan 的位置\n",
    "data[nan_indices] = np.take(col_mean, np.where(nan_indices)[1])# 通过替换操作，可以将 nan 替换为列平均值或行平均值\n",
    "print(\"替换列平均值后的数组:\\n\", data)\n",
    "# 利用多个接口，使代码简短，高级"
   ],
   "id": "af6a6722b1e425fc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "替换列平均值后的数组:\n",
      " [[1.  2.  7.5]\n",
      " [4.  5.  6. ]\n",
      " [7.  8.  9. ]]\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T16:26:22.058294Z",
     "start_time": "2025-01-07T16:26:22.053551Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 替换为列平均值，利用for循环遍历\n",
    "for i in range(data.shape[1]):\n",
    "    # 获取当前列数据\n",
    "    temp_col = data[:,i]\n",
    "    # 判断当前列的数据中是否含有nan\n",
    "    nan_num = np.count_nonzero(temp_col != temp_col)\n",
    "# 条件成立说明含有nan\n",
    "if nan_num != 0:\n",
    "    # 将这一列不为nan的数据拿出来\n",
    "    temp_col_not_nan = temp_col[temp_col == temp_col]\n",
    "    # 将nan替换成这一列的平均值\n",
    "    temp_col[np.isnan( temp_col )] = np.mean( temp_col_not_nan )\n",
    "print(\"替换列平均值后的数组:\\n\", data)"
   ],
   "id": "2c1028260e16d1d7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "替换列平均值后的数组:\n",
      " [[1.  2.  7.5]\n",
      " [4.  5.  6. ]\n",
      " [7.  8.  9. ]]\n"
     ]
    }
   ],
   "execution_count": 41
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
